package com.cbd.dao.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.cbd.dao.PersonalcarportDao;
import com.cbd.pojo.PageBean;
import com.cbd.pojo.Personalcarport;
@Component
public class PersonalcarportDaoImpl implements PersonalcarportDao {
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;

	@Override
	public PageBean findAllPortByLandLadyId(int ladyId,PageBean page) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Personalcarport p where p.landlady.ladyID=?");
		query.setInteger(0, ladyId);
		int num = (page.getCurPage()-1)*page.getPageSize();
		query.setFirstResult(num);
		query.setMaxResults(page.getPageSize());
		List<Personalcarport> list = query.list();
		page.setList(list);
		return page;
	}

	@Override
	public int addPort(Personalcarport personalcarport) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		int num = (int) session.save(personalcarport);
		return num;
	}

	@Override
	public List<Personalcarport> findAllUsablePort(int ladyId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Personalcarport> findAllNoPassPort(int ladyId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int findCarPortNum(int ladyId) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select count(*) from Personalcarport p where "
				+ " p.landlady.ladyID=?");
		query.setInteger(0, ladyId);
		Long l = (Long) query.uniqueResult();
		return l.intValue();
	}

	@Override
	public void delCarPortById(int id) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("delete from Personalcarport p where p.carportID = ?");
		query.setInteger(0, id);
		query.executeUpdate();
	}

	@Override
	public int checkCarportNO(String carportNO) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select count(*) from Personalcarport p where p.carportNO=?");
		query.setString(0, carportNO);
		Long l = (Long) query.uniqueResult();
		return l.intValue();
	}

	@Override
	public int checkCarNO(String carNO, String carportAddress) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select count(*) from Personalcarport p where "
				+ " p.carNO=? and p.carportAddress = ?");
		query.setString(0, carNO);
		query.setString(1, carportAddress);
		Long l = (Long) query.uniqueResult();
		return l.intValue();
	}

	@Override
	public List<Personalcarport> getAllCarPort(int ladyID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(" from Personalcarport p "
				+ " where p.type=true and p.isrent = false and p.landlady.ladyID=? "
				+ " and p.carportID not in (select distinct(r.personalcarport.carportID) "
				+ " from RentInfo r where r.type = true )");
		query.setInteger(0, ladyID);
		List<Personalcarport> list = query.list();
		return list;
	}

	@Override
	public void updateCarPortIsRentById(int carPortID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("update Personalcarport p set p.isrent = true where p.carportID = ?");
		query.setInteger(0, carPortID);
		query.executeUpdate();
	}

	@Override
	public List<Object[]> findCarPortsByTimes(String startTime,
			String endTime ,int userID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Date nowDate = new Date();
		//9个字段  
		String hql = "select ri.rentInfoID,pc.carportAddress,pc.carNO,ri.startTime,ri.endTime,"
				+ "u.userName,ll.ladyDescribe,ll.ladyRP,ri.price from Personalcarport pc,RentInfo ri,Landlady ll,"
				+ " User u where pc.carportID = ri.personalcarport.carportID "
				+" and pc.landlady.ladyID = ll.ladyID and ll.user.userID = u.userID"
				+ " and ri.rentInfoID not in (select rei.rentInfoID from Purpose pp,RentInfo rei where "
				+ " pp.rentInfo.rentInfoID = rei.rentInfoID and pp.isDeal = false and "
				+ " pp.renter.user.userID = ? ) and pc.type = true "
				+ " and pc.isrent = false and ll.ladyType = true and ri.endTime > ?";
		StringBuilder sBuilder = new StringBuilder(hql);
		//判断页面上传过来的开始时间和结束时间
		if(startTime != null && startTime != ""){
			sBuilder.append(" and ri.startTime <= ? ");
		}
		if(endTime != null && endTime != ""){
			sBuilder.append(" and ri.endTime >=? ");
		}
		Query query = session.createQuery(sBuilder.toString());
		query.setParameter(0, userID);
		query.setParameter(1, nowDate);
		if(startTime != null && startTime != "" || endTime != null && endTime != ""){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			if(startTime != null && startTime != "" && endTime != null && endTime != ""){
				//转换日期
				try {
					Date startDate = sdf.parse(startTime);
					Date endDate = sdf.parse(endTime);
					query.setParameter(2, startDate);
					query.setParameter(3, endDate);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				if(startTime != null && startTime != ""){
					try {
						Date startDate = sdf.parse(startTime);
						query.setParameter(2, startDate);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else{
					try {
						Date endDate = sdf.parse(endTime);
						query.setParameter(2, endDate);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		return query.list();
	}

	@Override
	public List<Personalcarport> findAllUnAbelPort() {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(" from Personalcarport where type = 0");
		
		return query.list();
	}

	@Override
	public void accept(int parseInt) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query =session.createQuery("update Personalcarport set type = 1 where carportID =?");
		query.setParameter(0, parseInt);
		query.executeUpdate();
	}
	
	@Override
	public PageBean findUnableProtByLadyID(PageBean pageBean, int parseInt) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Personalcarport where type = 0 and landlady.ladyID = ?");
		query.setParameter(0, parseInt);
		query.setFirstResult((pageBean.getCurPage()-1)*pageBean.getPageSize());
		query.setMaxResults(pageBean.getPageSize());
		List<Personalcarport> list = findAllUnablePortByLadyID(parseInt);
		int size =list.size();
		pageBean.setCountPage(size%pageBean.getPageSize()==0?size/pageBean.getPageSize():size/pageBean.getPageSize()+1);
		pageBean.setList(query.list());
		return pageBean;
	}

	@Override
	public List<Personalcarport> findAllUnablePortByLadyID(int ladyID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Personalcarport where type = 0 and landlady.ladyID = ?");
		query.setParameter(0, ladyID);
		return query.list();
	}

	@Override
	public List<Object[]> findOnePageCarPortByTime(int pageSize,int currentPage,
			String startTime, String endTime, int userID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Date nowDate = new Date();
		//9个字段  
		String hql = "select ri.rentInfoID,pc.carportAddress,pc.carNO,ri.startTime,ri.endTime,"
				+ "u.userName,ll.ladyDescribe,ll.ladyRP,ri.price from Personalcarport pc,RentInfo ri,Landlady ll,"
				+ " User u where pc.carportID = ri.personalcarport.carportID "
				+" and pc.landlady.ladyID = ll.ladyID and ll.user.userID = u.userID"
				+ " and ri.rentInfoID not in (select rei.rentInfoID from Purpose pp,RentInfo rei where "
				+ " pp.rentInfo.rentInfoID = rei.rentInfoID and pp.isDeal = false and "
				+ " pp.renter.user.userID = ? ) and pc.type = true "
				+ " and pc.isrent = false and ll.ladyType = true and ri.endTime > ?";
		StringBuilder sBuilder = new StringBuilder(hql);
		//判断页面上传过来的开始时间和结束时间
		if(startTime != null && startTime != ""){
			sBuilder.append(" and ri.startTime <= ? ");
		}
		if(endTime != null && endTime != ""){
			sBuilder.append(" and ri.endTime >=? ");
		}
		Query query = session.createQuery(sBuilder.toString());
		query.setParameter(0, userID);
		query.setParameter(1, nowDate);
		if(startTime != null && startTime != "" || endTime != null && endTime != ""){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			if(startTime != null && startTime != "" && endTime != null && endTime != ""){
				//转换日期
				try {
					Date startDate = sdf.parse(startTime);
					Date endDate = sdf.parse(endTime);
					query.setParameter(2, startDate);
					query.setParameter(3, endDate);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				if(startTime != null && startTime != ""){
					try {
						Date startDate = sdf.parse(startTime);
						query.setParameter(2, startDate);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else{
					try {
						Date endDate = sdf.parse(endTime);
						query.setParameter(2, endDate);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		int startItem = pageSize*(currentPage - 1);
		query.setFirstResult(startItem);
		query.setMaxResults(pageSize);
		return query.list();
	}

	@Override
	public void updateCarPortIsRentFalseById(int carPortID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("update Personalcarport p set p.isrent = false where p.carportID = ?");
		query.setInteger(0, carPortID);
		query.executeUpdate();
	}

}
